
import { _decorator, Component, director, Game, VERSION, game } from 'cc';
import { App } from '../src/App';
import { EventConst } from '../src/core/Common';
const { ccclass } = _decorator;
 
@ccclass('MainScene')
export class MainScene extends Component {
    start () {
        // native 层回调
        director.on(EventConst.App_NATIVE_MESSAGE,(flag:string,data?:any)=>{
            App.Event.emit(flag,data);
        },this);
        this.node.on(Game.EVENT_HIDE, () => {
            App.Event.emit(EventConst.APP_HIDE);
        });
        this.node.on(Game.EVENT_SHOW, () => {
            App.Event.emit(EventConst.APP_SHOW);
        });
        this.node.on(Game.EVENT_RESTART, () => {
        });

        game.addPersistRootNode(this.node);
        App.init('config',(is:boolean)=>{
            App.Audio.init(this.node);

            let group:string[] = [];
            for (let i = 0; i < 8; ++i) group.push(`music/bgm0${i}`);
            //App.Audio.playGroup(group);
            //App.Audio.play('music/bgm00',false)
        });

        // 异常捕获
        const mainVer = parseInt(VERSION.split('.')[0]);
        if (mainVer < 3){
            // @ts-ignore
            window.__errorHandler = (file: any, line: any, errorMessage:any, error: any)=>{
                const exception = {
                    file:file,
                    line:line,
                    errorMessage:errorMessage,
                    error:error
                };
                const exce = JSON.stringify(exception);
                console.log('<=======>', exce);
                if (App.getValue('debug')){
                    App.showMessageBox(App.Util.format(App.i18n.getString('comMsgBox'),exce));
                }
            }
        } else {
            const consoleError = window.console.error;
            window.console.error = function(){
                console.log('<=======>', JSON.stringify(arguments));
                consoleError && consoleError.apply(window, arguments);
                if (App.getValue('debug')){
                    App.showMessageBox(App.Util.format(App.i18n.getString('comMsgBox'),JSON.stringify(arguments)));
                }
            }
        }
    }

    onEnable () {
        App.Audio.keepPlay();
    }
}
